<?php
R('Login','chklogin');
// 本文档自动生成，仅供测试运行
class IndexAction extends Action
{
    /**
    +----------------------------------------------------------
    * 默认操作
    +----------------------------------------------------------
    */
    public function index()
    {
    	$this->assign('title','后台管理');
		$this->display();
    	

    }

    //7天内过期列表
    public function exp7()
    {
    	$DB_lm = M('class');
    	$class = $DB_lm->select();
    	//创建用于在前台显示多个栏目名的栏目索引
    	foreach($class as $v)
    	{
    		$class_forname[$v['id']] = $v['classname'];
    	}
    	$d7 = 24*60*60*7 + time();
    	$DB = M('items');
    	$list = $DB->where('expiration <'.$d7)->select();
    	foreach($list as $k=>$v)
    	{
    		$arr = split(',',$v['classid']);
    		$cname = null;
    		foreach($arr as $vv)
    		{
    			if($cname) $cname .="<br />".$class_forname[$vv]; else $cname = $class_forname[$vv];
    		}
    		$list[$k]['classname'] = $cname;
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
	//栏目列表
    public function listclass()
    {
    	$db = M('class');
    	$arr = $db->select();
    	$this->assign('list',$arr);
		$this->assign('delurl',U('delclass'));
		$this->assign('addurl',U('addclass'));
		$this->assign('editurl',U('editclass'));
    	$this->display('listclass');

    }
	//删除栏目
	public function delclass()
	{
		$DB = M("class");	
		$id = $_POST['id'];
		$result = $DB->where('id='.$id)->delete();
		if($result)
		{
			$this->ajaxreturn('删除成功',true);

		}
		else
		{
			$this->ajaxreturn('删除错误',false);
		}
	}

	//发送ajax数据到前端
	protected function ajaxreturn($msg,$state)
	{
		$arr['msg'] = $msg;
		$arr['state'] = $state ? 1 : 0;
		echo json_encode($arr);
		exit();
	}
	//添加栏目处理函数
	public function addclass()
	{
		$DB = M('class');
		$DB->create();
		$DB->add();
		$this->redirect('listclass');
	}
	//修改栏目显示函数
	public function editclass()
	{
		$DB = M('class');
		$result = $DB->where('id='.$_REQUEST['id'])->find();
		$this->assign('data',$result);
		$this->assign('modifyclassurl',U('modifyclass'));
		$this->display();
	}
	//修改栏目处理函数
	public function modifyclass()
	{
		$DB = M('class');
		$DB->create();
		$DB->save();
		$this->redirect('listclass');
	}
	
	
	//=====================信息处理函数段=========================//
	//显示信息
	public function showmsg()
	{
		//显示栏目
		$DB_lm = M('class');
		$class = $DB_lm->select();
		$this->assign('class',$class);		
		//创建用于在前台显示多个栏目名的栏目索引
		foreach($class as $v)
		{
			$class_forname[$v['id']] = $v['classname'];
		}
		$DB = M('items');
		if($_REQUEST['class'] != 0)
		{
			$where['_string'] = "concat(',',classid,',') like '%,{$_REQUEST['class']},%'";
		}
		if(($s_weburl = trim($_REQUEST['weburl'])) !='')
		{
			$where['linkurl'] = array('like','%'.$s_weburl.'%');
		}
		if(($s_webname = trim($_REQUEST['webname'])) !='')
		{
			$where['linkname'] = array('like','%'.$s_webname.'%');
		}
		if(($s_servermsg = trim($_REQUEST['servermsg'])) !='')
		{
			$where['servermsg'] = array('like','%'.$s_servermsg.'%');
		}
		if(($s_notice = trim($_REQUEST['notice'])) !='')
		{
			$where['notice'] = array('like','%'.$s_notice.'%');
		}
		if(($s_contact = trim($_REQUEST['contact'])) !='')
		{
			$where['contact'] = array('like','%'.$s_contact.'%');
		}
		if((($d1 = $_REQUEST['expiration1']) !='') && (($d2 = $_REQUEST['expiration2']) !=''))
		{
			$where['expiration'] = array('between',array($this->stod($d1),$this->stod($d2)));
		}
		if(($q_d = $_REQUEST['q_d']) !='-1' && $q_d !='')
		{
			$d = time() + 60*60*24*($_REQUEST['q_d']);
			$where['expiration'] = array('lt',$d);
		}
		if(isset($_REQUEST['vlid']))
		{
			
		}
		else
		{
			$where['vlid'] = array('eq',0);
		}
		//$where['vlid'] = array('eq',0);

		import('ORG.Util.Page');
		$count = $DB->where($where)->count();	//查询满足要求的总记录数
		$Page = new Page($count,10);
		$show = $Page->show();
		$list = $DB->where($where)->order('vlid asc,expiration asc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($list as $k=>$v)
		{
			$arr = split(',',$v['classid']);
			$cname = null;
			foreach($arr as $vv)
			{
				if($cname) $cname .="<br />".$class_forname[$vv]; else $cname = $class_forname[$vv]; 
			}
			$list[$k]['classname'] = $cname;
		}
		$this->assign('list',$list);//列表
		$this->assign('page',$show);//分页输出
		$this->assign('addurl',U('additem'));		//添加新条目地址
		$this->assign('editurl',U('edititem'));		//修改条目地址
		$this->assign('renewurl',U('renew'));		//续费地址
		$this->assign('delurl',U('delitem'));			//删除某条记录
		$this->assign('now',time());				//当前时间戳
		
		$this->display();
	}
	//删除条目
	public function delitem()
	{
		$id= $_REQUEST['id'];
		$DB =M('items');
		$DB->where('id='.$id)->delete();
		$this->ajaxreturn('删除成功',true);
	}
	
	//添加新条目 
	public function additem()
	{
		if(isset($_POST['addmsg']))
		{
			$DB = D('items');
			$DB->create();
			$id = $DB->add();
			
			$DB_money = M('money');
			$data['userid'] = $id;
			$data['addmoney'] = $_REQUEST['money'] * $_REQUEST['month'];
			$data['addtime'] = time();
			$DB_money->add($data);
			if($id)
			{
				echo "添加成功!<br /><a href='".U('additem')."'>继续添加</a> 或者 <a href='".U('showmsg')."'>返回列表</a>";
			}
			
			
		}
		else
		{
		//显示栏目
			$DB_lm = M('class');
			$class = $DB_lm->select();
			$this->assign('class',$class);
			$this->display('additem');
		}
	}
	//修改条目
	public function edititem()
	{
		if(isset($_POST['modifymsg']))
		{
			$DB = D('items');
			$DB->create();
			$DB->save();
			$this->redirect('showmsg','',1,'修改成功，1秒后返回列表页');
			
			
		}
		else
		{
			$DB_lm = M('class');
			$class = $DB_lm->select();
			$this->assign('class',$class);
			$id = $_REQUEST['id'];
			$DB = D('items');
			$result = $DB->where('id = '.$id)->find();
			$this->assign('data',$result);
			$this->display('modifyitem');
		}
	}
	//续费
	public function renew()
	{
		if (isset($_REQUEST['renew']))
		{
			$data['money'] = $_REQUEST['m1'];
			$data['expiration'] = $this->stod($_REQUEST['expiration']);
			$data['id'] = $_REQUEST['id'];
			$DB_item = M('items');
			$DB_item->save($data);
			
			$DB_money = M('money');
			$d2['addmoney'] = $_REQUEST['money'];
			$d2['userid'] = $_REQUEST['id'];
			$d2['addtime'] = time();
			$DB_money->add($d2);
			$this->redirect('showmsg','',1,'续费成功！1秒后转到列表页面');
		}
		else
		{
				
			
			$id = $_REQUEST['id'];
			$DB_item = D('itemsview');
			$itemwhere['id'] = array('eq',$id);
			$iteminfo = $DB_item->where($itemwhere)->find();
			if(!empty($iteminfo))
			{
				$where['userid'] = array('eq',$id);
				$DB_money = M('money');
				import('ORG.Util.Page');
				$count = $DB_money->where($where)->count();	//查询满足要求的总记录数
				$Page = new Page($count,10);
				$show = $Page->show();
				$money_list = $DB_money->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
				$this->assign('money_list',$money_list);
				$this->assign('page',$show);
				$this->assign('data',$iteminfo);
				$this->display('renew');
			}else
			{
				var_dump($DB_item);
				exit();
				$this->redirect('showmsg','',3,'没有此数据，3秒后转到列表');
			}
		}
	}
	//显示消费金额
	public function moneylist()
	{
		$DB_lm = M('class');
		$class = $DB_lm->select();
		$this->assign('class',$class);
		import('ORG.Util.Page');
		//显示每个栏目下每个网站总的消费数据
		if(($_REQUEST['class'] != '-1' && $_REQUEST['class'] !='') || $_REQUEST['sea_txt'] !='')
		{
			$where['classid'] = array('eq',$_REQUEST['class']);
			if($_REQUEST['sea_txt']!='')
			{
				unset($where);
				$where['linkname'] = array('like','%'.$_REQUEST['sea_txt'].'%');
			}
			$DB_item = D('itemsview');		//消费金额视图
			$count = $DB_item->where($where)->count();	//查询满足要求的总记录数
			$Page = new Page($count,10);
			$show = $Page->show();
			$money_list = $DB_item->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
			$DB_m = M('money');
			foreach($money_list as $k=>$v)
			{
				$money_list[$k]['moneys'] = $DB_m->where('userid='.$v['id'])->sum('addmoney');
			}
			$this->assign('money_list',$money_list);
			$this->assign('page',$show);
			$this->assign('type','list');
			
		}
		
		
		if(isset($_REQUEST['mid']))
		{
			$DB = D('moneyview');
			unset($where);
			$where['itemid'] = array('eq',$_REQUEST['mid']);
			$count = $DB->where($where)->count();	//查询满足要求的总记录数
			$Page = new Page($count,10);
			$show = $Page->show();
			$money_list = $DB->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
			$sum = $DB->where($where)->sum('addmoney');
			$this->assign('list',$money_list);
			$this->assign('page',$show);
			$this->assign('type','cnt');
			$this->assign('sum',$sum);
		}
		/**列出汇总**/
		$DB_money = D('moneyview');
		$all = $DB_money->sum('addmoney');
		$DB=M();
		
		foreach($class as $k=>$v)
		{
			$class_allsql['classid'] = array('eq',$v['id']);
			$classall[]=array('classname'=>$v['classname'],'id'=>$v['id'],'sum'=>$DB_money->where($class_allsql)->sum('addmoney'));
		}
		$this->assign('all',$all);					//总数
		$this->assign('c_all',$classall);			//分栏目统计数据		
		
		$this->display();
		
		
	}

	//更改新密码
	public function mpassword()
	{
		if(isset($_REQUEST['change']))
		{
			$DB = M('admin');
			$data['password'] = md5($_REQUEST['password']);
			$DB->where('id='.$_SESSION['member']['id'])->save($data);
			echo '更改成功！';
		}
		else
		{
			$this->display();
		}
	}
	
	
	//转换 2012-2-2 这样的日期到时间戳
	private function stod($d)
	{
		$d = split('-', $d);
		return mktime(0,0,0,$d[1],$d[2],$d[0]);
	}
}
?>
